import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../controllers/settings_controller.dart';

class SettingsPage extends StatelessWidget {
  const SettingsPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SettingsController>(
      init: SettingsController(),
      builder: (controller) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('设置'),
          ),
          body: SafeArea(
            child: SingleChildScrollView(
              padding: EdgeInsets.all(16.w),
              child: Column(
                children: [
                  // 外观设置
                  _buildSettingSection(
                    context,
                    title: '外观设置',
                    items: [
                      _buildSettingItem(
                        context,
                        icon: Icons.dark_mode,
                        title: '暗黑模式',
                        trailing: Obx(() => Switch(
                          value: controller.isDarkMode.value,
                          onChanged: (value) => controller.toggleDarkMode(),
                        )),
                      ),
                      _buildSettingItem(
                        context,
                        icon: Icons.color_lens,
                        title: '主题颜色',
                        subtitle: '选择主题颜色',
                        showArrow: true,
                        onTap: () => controller.showThemeColorPicker(),
                      ),
                      _buildSettingItem(
                        context,
                        icon: Icons.language,
                        title: '语言',
                        subtitle: controller.currentLanguage.value,
                        showArrow: true,
                        onTap: () => controller.showLanguagePicker(),
                      ),
                    ],
                  ),
                  
                  SizedBox(height: 24.h),
                  
                  // 通知设置
                  _buildSettingSection(
                    context,
                    title: '通知设置',
                    items: [
                      _buildSettingItem(
                        context,
                        icon: Icons.notifications,
                        title: '推送通知',
                        trailing: Obx(() => Switch(
                          value: controller.pushNotificationEnabled.value,
                          onChanged: (value) => controller.togglePushNotification(),
                        )),
                      ),
                      _buildSettingItem(
                        context,
                        icon: Icons.vibration,
                        title: '震动',
                        trailing: Obx(() => Switch(
                          value: controller.vibrationEnabled.value,
                          onChanged: (value) => controller.toggleVibration(),
                        )),
                      ),
                      _buildSettingItem(
                        context,
                        icon: Icons.volume_up,
                        title: '声音',
                        trailing: Obx(() => Switch(
                          value: controller.soundEnabled.value,
                          onChanged: (value) => controller.toggleSound(),
                        )),
                      ),
                    ],
                  ),
                  
                  SizedBox(height: 24.h),
                  
                  // 隐私设置
                  _buildSettingSection(
                    context,
                    title: '隐私设置',
                    items: [
                      _buildSettingItem(
                        context,
                        icon: Icons.fingerprint,
                        title: '生物识别',
                        trailing: Obx(() => Switch(
                          value: controller.biometricEnabled.value,
                          onChanged: (value) => controller.toggleBiometric(),
                        )),
                      ),
                      _buildSettingItem(
                        context,
                        icon: Icons.lock,
                        title: '自动锁定',
                        subtitle: controller.autoLockTime.value,
                        showArrow: true,
                        onTap: () => controller.showAutoLockPicker(),
                      ),
                      _buildSettingItem(
                        context,
                        icon: Icons.analytics,
                        title: '数据分析',
                        trailing: Obx(() => Switch(
                          value: controller.analyticsEnabled.value,
                          onChanged: (value) => controller.toggleAnalytics(),
                        )),
                      ),
                    ],
                  ),
                  
                  SizedBox(height: 24.h),
                  
                  // 存储设置
                  _buildSettingSection(
                    context,
                    title: '存储设置',
                    items: [
                      _buildSettingItem(
                        context,
                        icon: Icons.cached,
                        title: '清除缓存',
                        subtitle: controller.cacheSize.value,
                        showArrow: true,
                        onTap: () => controller.clearCache(),
                      ),
                      _buildSettingItem(
                        context,
                        icon: Icons.download,
                        title: '下载设置',
                        subtitle: '管理下载文件',
                        showArrow: true,
                        onTap: () => controller.openDownloadSettings(),
                      ),
                      _buildSettingItem(
                        context,
                        icon: Icons.backup,
                        title: '备份与恢复',
                        subtitle: '数据备份设置',
                        showArrow: true,
                        onTap: () => controller.openBackupSettings(),
                      ),
                    ],
                  ),
                  
                  SizedBox(height: 24.h),
                  
                  // 关于
                  _buildSettingSection(
                    context,
                    title: '关于',
                    items: [
                      _buildSettingItem(
                        context,
                        icon: Icons.info,
                        title: '关于应用',
                        subtitle: 'v${controller.appVersion.value}',
                        showArrow: true,
                        onTap: () => controller.openAbout(),
                      ),
                      _buildSettingItem(
                        context,
                        icon: Icons.feedback,
                        title: '意见反馈',
                        subtitle: '提交问题和建议',
                        showArrow: true,
                        onTap: () => controller.openFeedback(),
                      ),
                      _buildSettingItem(
                        context,
                        icon: Icons.update,
                        title: '检查更新',
                        subtitle: '检查新版本',
                        showArrow: true,
                        onTap: () => controller.checkUpdate(),
                      ),
                    ],
                  ),
                  
                  SizedBox(height: 32.h),
                  
                  // 重置设置按钮
                  SizedBox(
                    width: double.infinity,
                    child: OutlinedButton(
                      onPressed: () => controller.resetSettings(),
                      style: OutlinedButton.styleFrom(
                        foregroundColor: Theme.of(context).colorScheme.error,
                        side: BorderSide(color: Theme.of(context).colorScheme.error),
                        padding: EdgeInsets.symmetric(vertical: 16.h),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8.r),
                        ),
                      ),
                      child: Text(
                        '重置设置',
                        style: TextStyle(
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  Widget _buildSettingSection(
    BuildContext context, {
    required String title,
    required List<Widget> items,
  }) {
    return Container(
      width: double.infinity,
      decoration: BoxDecoration(
        color: Theme.of(context).colorScheme.surface,
        borderRadius: BorderRadius.circular(12.r),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10.r,
            offset: Offset(0, 2.h),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.all(16.w),
            child: Text(
              title,
              style: TextStyle(
                fontSize: 16.sp,
                fontWeight: FontWeight.w600,
                color: Theme.of(context).colorScheme.onSurface,
              ),
            ),
          ),
          const Divider(height: 1),
          ...items,
        ],
      ),
    );
  }

  Widget _buildSettingItem(
    BuildContext context, {
    required IconData icon,
    required String title,
    String? subtitle,
    Widget? trailing,
    bool showArrow = false,
    VoidCallback? onTap,
  }) {
    return InkWell(
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.all(16.w),
        child: Row(
          children: [
            Icon(
              icon,
              size: 20.w,
              color: Theme.of(context).colorScheme.primary,
            ),
            SizedBox(width: 12.w),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    title,
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: Theme.of(context).colorScheme.onSurface,
                    ),
                  ),
                  if (subtitle != null) ...[
                    SizedBox(height: 2.h),
                    Text(
                      subtitle,
                      style: TextStyle(
                        fontSize: 12.sp,
                        color: Colors.grey[600],
                      ),
                    ),
                  ],
                ],
              ),
            ),
            if (trailing != null) ...[
              SizedBox(width: 8.w),
              trailing,
            ],
            if (showArrow) ...[
              SizedBox(width: 8.w),
              Icon(
                Icons.arrow_forward_ios,
                size: 16.w,
                color: Colors.grey[400],
              ),
            ],
          ],
        ),
      ),
    );
  }
}